package com.d2rv.offscreen;

import codeanticode.glgraphics.GLSLShader;
import com.d2rv.guide.GoalManager;
import com.d2rv.model.CityModelManager;
import com.d2rv.settings.Settings;
import com.d2rv.skybox.SkyboxManager;
import com.d2rv.user.UserManager;
import java.io.File;
import processing.core.PApplet;
import processing.core.PVector;

/**
 * This class is the manager of the anaglyphe mode.
 *
 * @author Darrieutort A., Dupouy A., Rossignol L., Van Poucke A.
 */
public class OffScreenUserMono extends OffScreen3D {

    private UserManager usermanager;
    private CityModelManager citymodelmanager;
    private SkyboxManager skyboxmanager;
    private GoalManager goalmanager;
    private GLSLShader lightshader;

    /**
     * Default constructor
     *
     * @param parent the current application
     * @param width the width of the offscreen
     * @param height the height of the offscreen
     * @param usermanager the manager of the user 
     * @param citymodelmanager the manager of the city model
     * @param skyboxmanager the manager of the skybox
     * @param goalmanager the manager of the goal
     */
    public OffScreenUserMono(PApplet parent, int width, int height,
            UserManager usermanager, CityModelManager citymodelmanager, SkyboxManager skyboxmanager, GoalManager goalmanager) {
        super(parent, width, height,
                new PVector(usermanager.user().headposition().x, usermanager.user().headposition().y, usermanager.user().headposition().z),
                new PVector(0, 0, usermanager.user().headposition().z),
                new PVector(0, 1, 0));
        this.usermanager = usermanager;
        this.citymodelmanager = citymodelmanager;
        this.skyboxmanager = skyboxmanager;
        this.goalmanager = goalmanager;
        this.lightshader = new GLSLShader(parent, parent.dataPath("shaders" + File.separator + "lightPerPixel.vert"), parent.dataPath("shaders" + File.separator + "lightPerPixel.frag"));
    }

    /**
     * Calls the draw method of the differents objects
     */
    public void draw() {
        Settings s = Settings.getInstance();
        
        beginDraw();
        background(225, 225, 225);

        this.cameramanager.updateCamera(this.usermanager.user().headposition(), new PVector(0, 0, this.usermanager.user().headposition().z));
        this.cameramanager.setCamera(this, 0);

        this.pushMatrix();

        this.rotateY(-this.usermanager.user().rotate());
        this.translate(-this.usermanager.user().position().x, 0, -this.usermanager.user().position().z);

        this.skyboxmanager.drawSkybox(this);
        
        
        if(this.light == true) {
            directionalLight(s.lightposition().x, s.lightposition().y, s.lightposition().z, s.lighttarget().x, s.lighttarget().y, s.lighttarget().z);
        }
        else {
            ambientLight(0, 0, 0);
        }
        this.lightshader.start();
        this.usermanager.drawArrowUser(this, this.goalmanager.currentGoal());
        this.citymodelmanager.drawCity(this);
        this.goalmanager.drawObjectif(this);
        this.lightshader.stop();
        this.usermanager.drawInstruction(this);
        this.popMatrix();

        endDraw();
    }

    /**
     *
     * loads the image in the texture of the offscreen
     * 
     * @param parent the current application
     * @param x the x coordinate of the left corner of the offscreen
     * @param y the y coordinate of the left corner of the offscreen
     */
    public void show(PApplet parent, int x, int y) {
        parent.image(getTexture(), x, y, this.width, this.height);
    }
}
